import 'package:flutter/material.dart';
import 'dart:async';

class AlertDialogDemo extends StatefulWidget {
  AlertDialogDemo({Key key}) : super(key: key);

  @override
  _AlertDialogDemoState createState() => _AlertDialogDemoState();
}

class _AlertDialogDemoState extends State<AlertDialogDemo> {

  String _choise = "Nothing";

  Future _openAlertDialog() async {
    final MyDislogState choiseState = await showDialog(
      context: context,
      barrierDismissible: false, // 设置为 false 强制用户操作，默认点击空白处自动关闭对话框
      builder: (context) => AlertDialog(
        title: Text("AlertDialog"),
        content: Text("Are you sure about this?"),
        actions: [
          FlatButton(
            onPressed: () {
              Navigator.pop(context, MyDislogState.Cancel);
            }, 
            child: Text("Cancal")
          ),
          FlatButton(
            onPressed: () {
              Navigator.pop(context, MyDislogState.OK);
            }, 
            child: Text("Ok")
          ),
        ],
      ),
    );

    switch(choiseState) {
      case MyDislogState.OK:
        setState(() {
          _choise = "OK";
        });
        break;
      case MyDislogState.Cancel:
        setState(() {
          _choise = "Cancel";
        });
        break;
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("AlertDialogDemo"),
        elevation: 0.0,
      ),
      body: Container(
        padding: EdgeInsets.all(16.0),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text("You Choise : $_choise"),
            SizedBox(height: 16.0),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                RaisedButton(
                  child: Text("Open AlertDialog"),
                  onPressed: _openAlertDialog,

                ),
              ],
            )
          ],
        ),
      ),
    );
  }
}

enum MyDislogState {
  OK, Cancel
}